package fafu.comedu.workerrepair.Tool;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.SeekBar;

/**
 * Created by Administrator on 2017/8/19.
 */

public class VerificationSeekBar extends android.support.v7.widget.AppCompatSeekBar {
    //这两个值为用算法使用的2空间复杂度
    private int index = 100;
    private boolean k = true;

    public VerificationSeekBar(Context context) {
        super(context);
    }

    public VerificationSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerificationSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            k = true;
            if (x - index > 35 && x-index < 70) {
                k = false;
//                Log.i("seek", "dispatchTouchEvent: down");
                return true;
            }
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (Math.abs(x - index) > 35 && Math.abs(x-index)< 70) {
                index = x;
                k = true;
            }else {
                k = false;
            }
        }
        if (event.getAction() == MotionEvent.ACTION_UP){
            if(getProgress() < 90) {
                setProgress(0);
                k = true;
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        index = 100;
                        setProgress(0);
                    }
                };
                Thread thread = new Thread(runnable);
                thread.start();
//                Log.i("seek", "dispatchTouchEvent: up");
                return k;
            }else{
                setProgress(100);
                index = -1;
            }
        }
//        Log.i("seek", "dispatchTouchEvent: out");
        if(k)
            return super.dispatchTouchEvent(event);
        else
            return false;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
